﻿namespace JobButenko.Models.BusinessLogicLayer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    public class VacancyRepository
    {
        private readonly JobDataContext dataContext;

        public VacancyRepository(JobDataContext dataContext)
        {
            this.dataContext = dataContext;
        }

        public void AddVacancy(Vacancy vacancy)
        {
            var dataManager = new DataManager();
            var employerId =
                dataManager.Employer.GetEmployerFromUserName(HttpContext.Current.User.Identity.Name).EmpoyerId;
            var newVacancy = new Vacancy
                                 {
                                     VacancyName = vacancy.VacancyName,
                                     ProfFieldId = vacancy.ProfFieldId,
                                     PostId = vacancy.PostId,
                                     SalaryId = vacancy.SalaryId,
                                     Info = vacancy.Info,
                                     DateOfStartWork = vacancy.DateOfStartWork,
                                     ValidityJob = vacancy.ValidityJob,
                                     EmploedId = employerId
                                 };
            this.dataContext.Vacancies.InsertOnSubmit(newVacancy);
            this.dataContext.SubmitChanges();
        }

        public IEnumerable<Vacancy> GetVacancyFromUser(string userName)
        {
            return this.dataContext.Vacancies.Where(vacancy => vacancy.Employer.User.UserName.Equals(userName));
        }

        public Vacancy GetVacancyFromId(int id)
        {
            return this.dataContext.Vacancies.SingleOrDefault(vacancy => vacancy.VacancyId.Equals(id));
        }

        public void UpdateVacancy(int id, Vacancy vacancy)
        {
            var vacancyDb = this.GetVacancyFromId(id);
            vacancyDb.VacancyName = vacancy.VacancyName;
            vacancyDb.ProfFieldId = vacancy.ProfFieldId;
            vacancyDb.PostId = vacancy.PostId;
            vacancyDb.SalaryId = vacancy.SalaryId;
            vacancyDb.Info = vacancy.Info;
            vacancyDb.DateOfStartWork = vacancy.DateOfStartWork;
            vacancyDb.ValidityJob = vacancy.ValidityJob;
            this.dataContext.SubmitChanges();
        }

        public void DeleteVacancy(int id)
        {
            var vacancy = this.GetVacancyFromId(id);

            this.dataContext.Vacancies.DeleteOnSubmit(vacancy);
            this.dataContext.SubmitChanges();
        }

        public List<Vacancy> SearchVacancy(Vacancy vacancy)
        {
            var resultVacansy = this.dataContext.Vacancies.Where(v => v.ValidityJob > DateTime.Now);
            if (vacancy.VacancyName != null)
            {
                resultVacansy = resultVacansy.Where(v => v.VacancyName.Contains(vacancy.VacancyName));
            }

            if (vacancy.ProfFieldId != 1)
            {
                resultVacansy = resultVacansy.Where(v => v.ProfFieldId.Equals(vacancy.ProfFieldId));
            }

            if (vacancy.PostId != 1)
            {
                resultVacansy = resultVacansy.Where(v => v.PostId.Equals(vacancy.PostId));
            }

            if (vacancy.SalaryId != 1)
            {
                resultVacansy = resultVacansy.Where(v => v.SalaryId.Equals(vacancy.SalaryId));
            }

            return resultVacansy.ToList();
        }
    }
}